/*
  Copyright 2006 by Sean Luke and George Mason University
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/

package sim.app.networktest;

import java.util.ArrayList;

import sim.engine.SimState;
import sim.field.continuous.Continuous2D;
import sim.field.network.Network;
import sim.util.Double2D;
import ventureco.agent.Agent;
import ventureco.agent.AgentImpl;
import ventureco.communication.Broadcast;
import ventureco.task.TaskCreator;

public /*strictfp*/ class NetworkTest extends SimState
    {
    public static final double XMIN = 0;
    public static final double XMAX = 800;
    public static final double YMIN = 0;
    public static final double YMAX = 600;

    public static final double DIAMETER = 8;
    public static int portNumber = 5555;

    public Continuous2D environment = null;
    public Network network = null;
    public static CustomNode nodes[] = new CustomNode[6];

    /** Creates a NetworkTest simulation with the given random number seed. */
    public NetworkTest(long seed)
        {
        super(seed);
        }

    boolean acceptablePosition( final CustomNode node, final Double2D location )
        {
    	
    	if( location.x < DIAMETER/2 || location.x > (XMAX-XMIN)-DIAMETER/2 ||
            location.y < DIAMETER/2 || location.y > (YMAX-YMIN)-DIAMETER/2 )
           return false;
    	
    	if( location.x > node.task.bottomRight.x || location.x < node.task.bottomLeft.x ||
                location.y > node.task.topLeft.y || location.y < node.task.bottomLeft.y )
    		return false;
    	
    	
        return true;
        }

    CustomNode makeNode(String name)
        {
        CustomNode node = new CustomNode(name,portNumber);
        environment.setObjectLocation(node, new Double2D( 
                random.nextDouble()*(XMAX-XMIN-DIAMETER)+XMIN+DIAMETER/2,
                random.nextDouble()*(YMAX-YMIN-DIAMETER)+YMIN+DIAMETER/2 ) );
        network.addNode(node);
        schedule.scheduleRepeating(node);
        return node;
        }

    public void start()
        {
        super.start();

        environment = new Continuous2D(16.0, (XMAX-XMIN), (YMAX-YMIN) );
        network = new Network();

        Agent a = new AgentImpl();
        int agentCount = a.getAgentCount();
        
        Broadcast broadcast = new Broadcast();
    	broadcast.start();
        
        // Create Nodes
        for(int i = 0;i<agentCount;i++)
        {
        	nodes[i] = makeNode("Agent-" + i);
        	System.out.println("Server Port Number of Agent-" + i + ": " +portNumber);
        	portNumber++;
        	try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//        nodes[0] = makeNode( "Agent-1");
//        nodes[1] = makeNode( "Agent-2");
//        nodes[2] = makeNode( "Agent-3");
//        nodes[3] = makeNode( "node3");
//        nodes[4] = makeNode( "node4");
//        nodes[5] = makeNode( "node5");

        }
        
               
        // Create Tasks
        TaskCreator tc = new TaskCreator();
        ArrayList<ventureco.task.Task> taskList ;
        taskList = tc.createTasks(800,600,agentCount);
        for(int i = 0;i<agentCount;i++) {
            nodes[i].task = taskList.get(i);
        	
        }
       
        // Create Edges
        for(int i = 0;i<agentCount;i++)
        {
        	for(int j = i + 1 ;j<agentCount;j++)
        	network.addEdge(nodes[i], nodes[j], i + "-" + j);
        }
      
        
        
        // Set Starting Positions
        for(int i = 0;i<agentCount;i++)
        {
        	if(ventureco.gui.menu.resumed == false)
        	 environment.setObjectLocation(nodes[i], new Double2D( 
                     nodes[i].task.startingLocation.x,
                     nodes[i].task.startingLocation.y) );
        	else
        		environment.setObjectLocation(nodes[i], new Double2D((double) AgentImpl.agents.get(i).getX(),
        				(double) AgentImpl.agents.get(i).getY()));
        
        
        }
       
        
/*      // for(int i = 0;i<agentCount;i++)
        {
        	CustomNode.sendMess(agentCount,4);
        	CustomNode.sendMess(agentCount,1);
        	CustomNode.sendMess(agentCount,2);
        	CustomNode.sendMess(agentCount,3);
        	CustomNode.sendMess(agentCount,5);
        	CustomNode.sendMess(agentCount,4);
        	CustomNode.sendMess(agentCount,1);
        	CustomNode.sendMess(agentCount,2);
        	CustomNode.sendMess(agentCount,3);
        	CustomNode.sendMess(agentCount,5);
        }
  */    
        
        
//        network.addEdge( nodes[2], nodes[3], "2-3" );
//        network.addEdge( nodes[3], nodes[4], "3-4" );
//        network.addEdge( nodes[4], nodes[0], "4-0" );
//        network.addEdge( nodes[0], nodes[5], "0-5" );
//        network.addEdge( nodes[1], nodes[5], "1-5" );
//        network.addEdge( nodes[2], nodes[5], "2-5" );
//        network.addEdge( nodes[3], nodes[5], "3-5" );
//        network.addEdge( nodes[4], nodes[5], "4-5" );
        }

    public static void main(String[] args)
        {
        doLoop(NetworkTest.class, args);
        System.exit(0);
        }    
    }
